﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamSystem.BLL;
using ExamSystem.Mdeol;
namespace ExamSystem.ht
{
    public partial class TQuestion_Edit : System.Web.UI.Page
    {
        private TkechengBLL tbl = new TkechengBLL();
        private TquestionBLL tb = new TquestionBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //获取id
                string bookid = Request.QueryString["id"];
                //查询图书集合
                List<Tquestion> bf = tb.selectquetionid(int.Parse(bookid));
                if (bf!=null)
                {
                    //查询的图书类型集合
                    List<Tkecheng> list = tbl.selectkckecheng();
                    Tkecheng bi = new Tkecheng(0, "--请选择--");
                    list.Insert(0, bi);

                    //绑定显示的数据
                    this.tkcId.DataSource = list;
                    this.tkcId.DataTextField = "tkcname";
                    this.tkcId.DataValueField = "tkcid";
                    this.tkcId.DataBind();

                    
                    //遍历图书集合
                    foreach(Tquestion tt in bf)
                    {
                        this.tkcId.Value = tt.TkcId.TkcId + "";
                        string tqtype = tt.Tqtype;
                        if(tqtype=="单选")
                        {
                            this.type1.Checked = true;
                        }
                        else
                        {
                            this.type2.Checked = true;
                        }
                        this.tqScore.Value = tt.TqScore+"";
                        this.tqName.InnerText = tt.TqName;
                        this.tqA.Value = tt.TqA;
                        this.tqB.Value = tt.TqB;
                        this.tqC.Value = tt.TqC;
                        this.tqD.Value = tt.TqD;
                        this.tqAnswer.Value = tt.TqAnswer;
                    }
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "数据不存在", "TQuestion_List.aspx");
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
              try
            {
                //获取文本显示的值
                string tkcid = this.tkcId.Value;

                //图书类型编号
                int booktypeid = 0;
                //判断图书编号是否等于零
                if (int.Parse(this.tkcId.Value) != 0)
                {
                    booktypeid = int.Parse(this.tkcId.Value);
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "id不合法");
                }

                  int tqId=int.Parse(Request.QueryString["id"]);
                  Tkecheng tkcId=new Tkecheng(booktypeid,"");
                  string tqName=this.tqName.InnerText;
                  string tqtype=this.type1.Value;
                  if(type2.Checked)
                  {
                      tqtype=this.type2.Value;
                  }
                  float tqScore=float.Parse(this.tqScore.Value);
                  string tqA=this.tqA.Value;
                  string tqB=this.tqB.Value;
                  string tqC=this.tqC.Value;
                  string tqD=this.tqD.Value;
                  string tqAnswer = this.tqAnswer.Value.ToUpper();
                  //验证
                  if(string.IsNullOrEmpty(tqName))
                  {
                      this.lbMsg.InnerText = "请输入试题名称";
                      return;
                  }
                  if (string.IsNullOrEmpty(tqA))
                  {
                      this.lbMsg.InnerText = "请输入选项A";
                      return;
                  }
                  if (string.IsNullOrEmpty(tqB))
                  {
                      this.lbMsg.InnerText = "请输入选项B";
                      return;
                  }
                  if (string.IsNullOrEmpty(tqC))
                  {
                      this.lbMsg.InnerText = "请输入选项C";
                      return;
                  }
                  if (string.IsNullOrEmpty(tqD))
                  {
                      this.lbMsg.InnerText = "请输入选项D";
                      return;
                  }
                  //判断类型和答案是否匹配
                  if(tqtype==this.type1.Value)
                  {
                      if(tqAnswer.Length!=1)
                      {
                          this.lbMsg.InnerText = "单选题，只能选择一个选项";
                          return;
                      }
                  }
                  else
                  {
                      if(tqAnswer.Length<=1)
                      {
                          this.lbMsg.InnerText = "多选题，至少选择两个选项";
                          return;
                      }
                  }
                  //入库

                  Tkecheng tk = new Tkecheng(int.Parse(tkcid),null);
                  Tquestion tq = new Tquestion(tqId,tk,tqName,tqtype,tqScore,tqA,tqB,tqC,tqD,tqAnswer);
                  int b=tb.update(tq);
                  if (b > 0)
                  {
                      util.FunHelper.LayerAlter(this, "修改成功", "TQuestion_List.aspx");
                  }
                  else
                  {
                      util.FunHelper.LayerAlter(this,"修改失败");
                  }
            }
            catch (Exception ex)
            {
                util.FunHelper.LayerAlter(this, ex+"");
            }
        }
    }
}